package scuts.core;

import scuts.core.types.LazyIterator;

class IntersperseArray 
{

  public static function intersperse < T > (a:Array<T>, b:T):Array<T> 
  {
    return if (a.length == 0) {
      [];
    } else {
      var res = [a[0]];
      for ( i in 1...a.length) {
        res.push(b);
        res.push(a[i]);
      }
      res;
    }
  }
}

class IntersperseIterator 
{

  public static function intersperse < T > (a:Iterator<T>, b:T):Iterator<T> 
  {
    var i = 0;
    return {
      hasNext : function () {
        return a.hasNext();
      },
      next : function () {
        return if (i++ % 2 == 0) a.next() else b;
      }
    }
  }
}

class IntersperseLazyIterator 
{

  public static function intersperse < T > (a:LazyIterator<T>, b:T):LazyIterator<T> 
  {
    return function () return IntersperseIterator.intersperse(a(), b);
  }
}